package org.jbmx.tests;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * Listing 3-1: SocketTest.java
 * This provides a learning tool for understanding Sockets in Java.
 * @author Core Java Volume II - Advanced Features, 8th edition
 *
 */
public class SocketTest {

  /**
   * SocketTest main.
   * @param args
   */
  public static void main(String[] args) {
    System.out.println("--Testing Sockets--");
    try {
      /* Create a socket with a physical address and port number. */
      Socket socket = new Socket("time-A.timefreq.bldrdoc.gov", 13);
      
      try {
        /* Create an input stream */
        InputStream inStream = socket.getInputStream();
        /* Retrieve data from the input stream. */
        Scanner in = new Scanner(inStream);
        /* Output data from the stream. */
        while(in.hasNextLine()) {
          String line = in.nextLine();
          System.out.println(line);
        }
      }
      finally {
        socket.close();
      }
    }
    catch (IOException e) {
      e.printStackTrace();
    }
  }

}
